<template>
  <div class="lc_wrap">
    <el-row class="CountreportTabel">
      
      <!-- 左侧树 -->
      <el-col :span="leftWidth">
        <div class="box treeBox">
          <basic-container>
            <lcTree @ready="ready" :deviceType="4"></lcTree>
          </basic-container>
        </div>
      </el-col>

      <!-- 内容 -->
      <el-col :span="rightWidth" class="table-wrapper">
        <overlay @changeTreeBox="changeTreeBox"></overlay>
        <basic-container>
          <div class="search-tools">

            <!-- 时间（月） -->
            <el-date-picker
              v-model="dateTime"
              type="month"
              size="small"
              value-format="yyyy-MM"
              :picker-options="pickerOption"
              placeholder="选择月份"
              :clearable="false">
            </el-date-picker>

            <!-- 搜索 -->
            <el-button 
              class="search_btn" 
              type="primary" 
              size="small" 
              icon="el-icon-search" 
              @click="searchReport">
              搜 索
            </el-button>

            <!-- 导出 -->
            <el-button
              type="warning"
              size="small"
              plain
              icon="el-icon-download"
              @click="handleExport"
              v-if="this.permission.loam_quality_export">
              导 出
            </el-button>
          </div>

          <!-- 表格内容 -->
          <avue-crud
            v-if="isTableShow"
            :option="option"
            :table-loading="loading"
            :data="data"
            v-model="form"
            ref="crud">
          </avue-crud>

        </basic-container>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getList } from "@/api/loam/qualityReport";
import { mapGetters } from "vuex";
import { xlsxDownloadExl } from "@/util/exportExcel";
import lcTree from "@/components/common/tree.vue";
import overlay from "@/components/common/overlay.vue";

export default {
  components: {
    lcTree, 
    overlay
  },
  data() {

    // 定义一个时间格式化函数
    Date.prototype.format = function(format) {
      var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds()
      };
      if (/(y+)/.test(format)) {
        format = format.replace(
          RegExp.$1,
          (this.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      }
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
        }
      }

      return format;
    };

    let _this = this;
    return {
      isTableShow: true,
      leftWidth: this.$leftWidth,
      rightWidth: this.$rightWidth,
      form: {},
      loading: false,
      option: {
        menu: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        calcHeight: 30,
        tip: false,
        printBtn: false,
        searchShow: true,
        refreshBtn: false,
        dialogClickModal: false,
        searchMenuSpan: 6,
        border: true,
        dialogClickModal: false,
        selection: false,
        index: true,
        header: false,
        column: [{
          label: "下辖",
          prop: "name",
          align: "center"
        }, {
          label: "优秀",
          prop: "excellentCount",
          align: "center"
        }, {
          label: "规范",
          prop: "standardCount",
          align: "center"
        }, {
          label: "合格",
          prop: "eligibleCount",
          align: "center"
        }, {
          label: "整改",
          prop: "rectifyCount",
          align: "center"
        }, {
          label: "达标率(%)",
          prop: "standardRate",
          align: "center"
        }, {
          label: "联动比(%)",
          prop: "linkageRatio",
          align: "center"
        }]
      },
      data: [],
      nodeId: null,
      deptInfoName: "",
      deptInfoId: "",
      dateTime: new Date().format("yyyy-MM"),
      pickerOption: {
        firstDayOfWeek: 1
      }
    };
  },
  computed: {
    ...mapGetters(["permission"])
  },
  methods: {

    // 左侧树加载事件
    ready(data) {
      this.nodeId = data.id;
      this.deptInfoName = data.title;
      this.deptInfoId = data.key;
      this.dateTime = new Date().format("yyyy-MM");
      this.onLoad();
    },

    // 导出报表
    handleExport() {
      var routeName = "治理质量统计";
      var title = this.deptInfoName + " " + this.dateTime.slice(0, 7);
      var cols = this.option.column;
      let tableData = [];
      this.data.forEach(item => {
        tableData.push({
          name: item.name + "",
          excellentCount: item.excellentCount + "",
          standardCount: item.standardCount + "",
          eligibleCount: item.eligibleCount + "",
          rectifyCount: item.rectifyCount + "",
          standardRate: item.standardRate + "",
          linkageRatio: item.linkageRatio + ""
        });
      });
      xlsxDownloadExl(routeName, title, cols, tableData);
    },

    // 左侧树收缩、展开
    changeTreeBox() {
      this.leftWidth = this.leftWidth == this.$leftWidth ? 0 : this.$leftWidth
      this.rightWidth = this.rightWidth == this.$rightWidth ? 24 : this.$rightWidth
    },

    // 搜索
    searchReport() {
      this.onLoad();
    },

    // 获取表数据
    onLoad() {
      this.loading = true;

      if (this.nodeId) {
        let myDate = this.dateTime.slice(0, 7);
        getList(this.nodeId, myDate).then(res => {
          if(res.data.success !== true) {
            return;
          }
          var resData = res.data.data;
          this.data = resData;
          this.loading = false;
        });
      }
    }
  }
};
</script>

<style lang="scss">
.CountreportTabel {
  .el-table__empty-block {
    width: 100% !important;
  }

  .avue-crud__menu {
    margin-bottom: $avue-tabel-menu-btn_margin_bottom;
  }

  .search-tools {
    padding-left: 0 !important;
    height: auto !important;
    margin-bottom: $common_margin_input_bottom;
  }

  .table-wrapper {
    .el-card__body {
      height: calc(100% - 35px);

      .avue-crud {
        height: calc(100% - 155px);
      }
    }

    .el-table__fixed {
      height: calc(100% - 12px) !important;
    }

    .el-table__fixed-right {
      height: calc(100% - 12px) !important;
    }
  }
}
</style>

<style lang="scss" scoped>
.map {
  width: 100%;
  height: 500px;
}

.box {
  height: 100%;
  margin-bottom: 0;
}

.el-row {
  height: calc(100% - 20px);
}

.el-col {
  height: 100%;
  overflow: auto;
}

.search-tools {
  height: 49px;
  display: flex;
  padding: 0 8px;
}

.search_btn {
  margin: 0px 0px 0px  10px;
}

.overlay {
  position: absolute;
  width: 200px;
  height: 100%;
  opacity: 0;
  display: flex;
  align-items: center;
  padding-left: 6px;

  .tree-tool {
    height: 34px;
    width: 18px;
    background: #fff;
    box-shadow: 2px 1px 4px #ccc;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    cursor: pointer;
    display: flex;
    padding: 6px 0;
    z-index: 9999;

    .tool-line {
      margin-left: 3px;
      height: 100%;
      border-left: 1px solid #ddd;
    }
  }
}

.overlay:hover {
  opacity: 1;
}
</style>